package com.hellohoper.script.config.admin.app.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hellohoper.assembly.common.utils.base.model.PageModel;
import com.hellohoper.script.config.admin.app.dao.ScriptConfigMapper;
import com.hellohoper.script.config.admin.app.dao.pojo.ScriptConfig;
import com.hellohoper.script.config.admin.app.zookeeper.ZookeeperService;
import com.hellohoper.script.config.admin.app.cache.ConfigCacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 配置管理操作service
 */
@Service
public class ScriptConfigService {

    @Autowired
    private ScriptConfigMapper scriptConfigMapper;

    @Autowired
    private ZookeeperService zookeeperService;

    @Autowired
    private ConfigCacheService configCacheService;

    /**
     * 根据逐渐查询配置
     *
     * @param id 主键
     * @return 配置
     */
    public ScriptConfig selectByPrimaryKey(Long id) {
        return scriptConfigMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据条件查询配置列表
     *
     * @param scriptConfig 配置条件
     * @return 配置列表
     */
    public List<ScriptConfig> selectConfigList(ScriptConfig scriptConfig) {
        List<ScriptConfig> scriptConfigs = scriptConfigMapper.selectScriptConfigPageList(scriptConfig);
        return scriptConfigs;
    }

    /**
     * 分页查询配置
     *
     * @param pageNum      第几页
     * @param pageSize     每页多少行
     * @param scriptConfig 配置条件
     * @return 配置列表
     */
    public PageModel<List<ScriptConfig>> selectPageList(int pageNum, int pageSize, ScriptConfig scriptConfig) {
        // 分页
        Page<Object> page = PageHelper.startPage(pageNum, pageSize, true);
        // 查询
        List<ScriptConfig> scriptConfigs = scriptConfigMapper.selectScriptConfigPageList(scriptConfig);
        // 返回结果
        return new PageModel<>(pageNum, pageSize, page.getTotal(), scriptConfigs);
    }

    @Transactional
    public int deleteByPrimaryKey(Long id) throws Exception {
        // 查询DB
        ScriptConfig scriptConfig = scriptConfigMapper.selectByPrimaryKey(id);
        // 同步配置到DB
        int delete = scriptConfigMapper.deleteByPrimaryKey(id);
        // 同步配置到缓存
        configCacheService.removeCache(scriptConfig);
        // 同步配置到ZK
        zookeeperService.deleteZkConfig(scriptConfig);
        return delete;
    }

    @Transactional
    public int insert(ScriptConfig record) throws Exception {
        Date createTime = new Date();
        // 添加脚本
        record.setCreateTime(createTime);
        record.setUpdateTime(createTime);
        record.setUserId(1L);
        // 插入数据库
        int insert = scriptConfigMapper.insert(record);
        // 同步配置到缓存
        configCacheService.setCache(record);
        // 同步配置到ZK
        zookeeperService.addZkConifig(record);
        return insert;
    }

    /**
     * 更新配置
     *
     * @param record 配置
     * @return 成功
     * @throws Exception E
     */
    @Transactional
    public int updateByPrimaryKeySelective(ScriptConfig record) throws Exception {
        // 同步配置到DB
        int update = scriptConfigMapper.updateByPrimaryKeySelective(record);
        // 同步配置到缓存
        configCacheService.updateCache(record);
        // 同步配置到ZK
        zookeeperService.updateZkConfig(record);
        return update;
    }

}